package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.Notices;
import ex.com.jm.food.service.CompanyNoticeService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;

public class CompanyNoticeAction extends ActionSupport {


	private static final long serialVersionUID = 1L;
 
	private CompanyNoticeService companyNoticeService;
	private Notices notices;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private String comId;
	private String noticeId;
	private String title;
	private String order;
	private String sort;
	private Integer status;
	private String s_date;
	private String e_date;
	private Map<String, String> dateMap;
	public CompanyNoticeService getCompanyNoticeService() {
		return companyNoticeService;
	}
	public void setCompanyNoticeService(CompanyNoticeService companyNoticeService) {
		this.companyNoticeService = companyNoticeService;
	}
	public Notices getNotices() {
		return notices;
	}
	public void setNotices(Notices notices) {
		this.notices = notices;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public String getComId() {
		return comId;
	}
	public void setComId(String comId) {
		this.comId = comId;
	}
	public String getNoticeId() {
		return noticeId;
	}
	public void setNoticeId(String noticeId) {
		this.noticeId = noticeId;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
	public Map<String, String> getDateMap() {
		return dateMap;
	}
	public void setDateMap(Map<String, String> dateMap) {
		this.dateMap = dateMap;
	}
	public String getS_date() {
		return s_date;
	}
	public void setS_date(String s_date) {
		this.s_date = s_date;
	}
	public String getE_date() {
		return e_date;
	}
	public void setE_date(String e_date) {
		this.e_date = e_date;
	}
	
	/**
	 * 列表页面
	 * @return
	 */
	public String noticeList(){
		dateMap=DateUtils.getDate();
		return "noticeList";
		
	}
	/**]
	 * 获取数据
	 */
	public void getNoticeList(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		int offset=(initPage-1)*pageRow;
		if(s_date==null||s_date.equals("")){
			s_date=DateUtils.get30Days();
		}
		if(e_date==null||e_date.equals("")){
			e_date=DateUtils.getCurrentDate();
		}
		List<Notices> notice=this.getCompanyNoticeService().getNotices(offset, pageRow, comId, title, s_date, e_date, order, sort);
		JSONArray jonotice=JSONArray.fromObject(notice);
		totalCount=this.getCompanyNoticeService().noticeCount(comId, title, s_date, e_date);
		JSONObject jo=JsonUtils.buildJson(jonotice, initPage, pageRow, totalCount);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
		    e.printStackTrace();
		}
		out.print(jo);
		out.flush();
		out.close();
	}
	/**
	 * 编辑
	 * @return
	 */
	public String editNotice(){
		if (noticeId!=null) {
			notices=this.getCompanyNoticeService().editNotices(noticeId);
		}
		return "edit";
	}
	
	public String saveNotice(){
		if(notices.getNoticeTitle()==null||notices.getNoticeTitle().equals("")){
			addFieldError("notices.noticeTitle", "标题不能为空");
			return "edit";
		}
		notices.setNoticeType(1);
		if (notices.getNoticeId()!=null&&!notices.getNoticeId().equals("")) {
			this.getCompanyNoticeService().updateNotices(notices);
			return "list";
		}
		notices.setCreated(DateUtils.getCurrentDate());
		this.getCompanyNoticeService().saveNotice(notices);
		return "list";
	}
	
	public String delNotice(){
		this.getCompanyNoticeService().delNotice(noticeId);
		return "list";
	}
	
	public String updateStatus(){
		this.getCompanyNoticeService().updateStatus(status, noticeId);
		return "list";
		
	}
	
	public void checkNotice(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		Notices notices=this.getCompanyNoticeService().checkNotices(comId);
		String msg="0";
		if (notices!=null) {
			msg="1";
		}else {
			msg="0";
		}
		JSONArray jo=JsonUtils.buildMessage(msg);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
		    e.printStackTrace();
		}
		out.print(jo);
		out.flush();
		out.close();
	}
	
	
}
